<?php
include_once 'DataForm/DataForm.php';
include_once 'DataGrid/Form/FormSearch.php';
include_once 'DataGrid/DataGrid.php';

class Admin_Model_Products extends NadebZend_Model_Crud
{
	public function __construct()
	{
    	$this->tb = new Application_Model_Db_Products();
    	
    	parent::__construct();
	}
	
	public function data_grid($params)
	{
		$modelPath = __LINKS__ . 'admin/' . $this->_params->controller;
		$data = $this->get_all();
		
		$grid = new DataGrid( 'nadeb-grid' );
		$grid->title( 'Listagem de Produtos' );
		$grid->primary( $this->primary );
		$grid->setData( $data );
		$grid->setAction( $modelPath.'/delete/' );
		$grid->setControllers( new DeleteButton('delete_button', 'Excluir') );
		$grid->setControllers( new Link('insert_button', 'Inserir', $modelPath . '/edit') );
		$grid->setTools( new LinkTool('Editar Detalhes', __LINKS__ . 'admin/details/list/rel', 'edit_rel') );
		$grid->setTools( new MoveTool('Mover', $modelPath . '/save-order') );
		$grid->setTools( new EditTool('Editar', $modelPath . '/edit') );
		$grid->setTools( new DeleteTool('Excluir', $modelPath . '/delete') );
		$grid->setHeader( array( 'Categoria' => 'tm150', 'Tipo' => 'tm100', 'Nome' => 'tm100', 'Genero', 'Exibir' ) );
		$grid->setRows( array(
							'idCategory' => new GArray( $this->categoryOptions() ), 
							'idFlag' => new GArray( $this->flagOptions() ), 
							'name', 
							'genderTags',
							'active' => new GSwap($modelPath, '1,0') ));
		
		return $grid->get();
	}
	
	public function data_form($id = null, $post = null)
	{
		$header      = HeaderController::get_instance();
		$header->css = "public/admin/css/frete.css";
		$header->js  = "public/admin/js/frete.js";
		
		
		$data = $id ? $this->get_one($id) : null;
		
		$form = new DataForm( 'nadeb-form' );
		$form->setData( $data );
		$form->title( 'Cadastro de Produtos' );
		$form->form()->action = __LINKS__ . 'admin/'. $this->_params->controller .'/edit/' . ( ($id) ? 'id/' . $id : '' );
		$form->add( new Select('idCategory','Categoria',$this->categoryOptions()) );
		$form->add( new Select('idFlag','Tipo',$this->flagOptions()) );
		$form->add( new InputText('name','Nome') );
		$form->add( new TextArea('description','Descrição') );
		$form->add( new Checkbox('genderTags','Genero',$this->genderOptions()) );
		$form->add( new Checkbox('themeTags','Temas',$this->themeOptions()) );
		$form->fieldset('frete');
		$form->label('nadebTitle', 'Frete');
		$form->add( new Select('nCdFormato','Tipo de Pacote',$this->nCdFormatoOptions()) );
		$form->label('labelTitu', '<b>Peso</b> <b>Altura</b> <b>Compr</b> <b>Largura</b> <b>Diametro</b>');
		$form->add( new InputText('nVlPeso',null, 0) );
		$form->add( new InputText('nVlAltura',null, 0) );
		$form->add( new InputText('nVlComprimento',null, 0) );
		$form->add( new InputText('nVlLargura',null, 0) );
		$form->add( new InputText('nVlDiametro',null, 0) );
		$form->fieldset('buttons');
		$form->add( new InputSubmit('btn_save', 'salvar') );
		$form->add( new InputButton('btn_cancel', 'cancelar') );
		
		if( $post )
		{
			$result = $this->save($id, $post, $form);
			return $result['message'];
		}
		else
		{
			return $form->get();
		}
	}
	
	public function search_form()
	{
		$form = new Nadeb_Data_Form('search-form');
		$form->form_action = __LINKS__ . 'admin/inscriptions/list/search/true/';
		$form->set_data     (null);
		$form->textsearch   ('hairStudioName','-');
        return $form->create_form()->get();
	}
	
	private function categoryOptions()
	{
		$tb = new Application_Model_Db_Categories();
		$data = $tb->fetchAll(null, 'order ASC');
		foreach ($data as $value) $result[$value->idCategory] = $value->name;
		
		return $result;
	}
	
	private function flagOptions()
	{
		$tb = new Application_Model_Db_Flags();
		$data = $tb->fetchAll(null, 'order ASC');
		foreach ($data as $value) $result[$value->idFlag] = $value->name;
		
		return $result;
	}
	
	private function genderOptions()
	{
		$tb = new Application_Model_Db_Gender();
		$data = $tb->fetchAll(null, 'order ASC');
		foreach ($data as $value) $result[$value->name] = $value->name;
		
		return $result;
	}
	
	private function themeOptions()
	{
		$tb = new Application_Model_Db_Theme();
		$data = $tb->fetchAll(null, 'order ASC');
		foreach ($data as $value) $result[$value->name] = $value->name;
		
		return $result;
	}
	
	private function nCdFormatoOptions()
	{
		$result = array(
			'3' => 'Envelope',
			'1' => 'Formato caixa/pacote',
			'2' => 'Formato rolo/prisma'
		);
		
		return $result;
	}
	
	public function breadCrumbs($action)
	{
		$bc = array(
			'list' => array(
				'' => 'home',
				'#' => 'Listagem de Produtos'
			),
			'edit' => array(
				'' => 'home',
				'products/list' => 'Listagem de Produtos',
				'#' => 'Cadastro de Produtos'
			)
		);
		
		return $bc[$action];
	}
}


